package project3;

public class Util {
	public enum CellType {
		NULL, // don't use this one
		CREATE,
		CREATED,
		RELAY, 
		DESTROY, 
		OPEN, 
		OPENED,
		OPEN_FAILED,
		CREATE_FAILED
	}

	public static final int CELL_LENGTH = 512;
	
	// returns the circuit id of a cell in a 512-byte long array
	public static int readCircId(byte[] data) {
		return getIntFromTwoBytes(data, 0);
	}
	
	// returns the cell type in a 512-byte long array
	public static CellType readCellType(byte[] data) {
		int cellTypeId = ((int) data[2] & 0xff);
		return CellType.values()[cellTypeId];
	}
	
	public static byte[] generateHeader(int circId, CellType cellType, int length) {
		byte[] ret = new byte[length];
		ret[0] = (byte) ((circId >> 8) & 0xFF);
		ret[1] = (byte) (circId & 0xFF);
		ret[2] = (byte) ((cellType.ordinal()) & 0xFF);
		return ret;
	}
	
	public static int getIntFromTwoBytes(byte[] data, int startPoint) {
		return ((int) (data[startPoint] & 0xFF) << 8) + ((int) (data[startPoint + 1] & 0xFF)) ;
	}
}
